using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
    public class tbHinhAnhDAL
    {
        private SqlConnection cn = null;
        public tbHinhAnhDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbHinhAnhDAL(tbHinhAnhDAL hinhanh)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public void Insert(tbHinhAnh hinhanh)
        {
            SqlCommand cm = new SqlCommand("sp_tbHinhAnh_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@HinhAnh", hinhanh.HinhAnh);
            cm.Parameters.AddWithValue("@MaSP", hinhanh.MaSP);
            cm.ExecuteNonQuery();
        }
        public void Update(tbHinhAnh hinhanh)
        {
            SqlCommand cm = new SqlCommand("sp_tbHinhAnh_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaHA", hinhanh.MaHA);
            cm.Parameters.AddWithValue("@HinhAnh", hinhanh.HinhAnh);
            cm.Parameters.AddWithValue("@MaSP", hinhanh.MaSP);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tbHinhAnh_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaHA", iD);
            cm.ExecuteNonQuery();
        }
        public tbHinhAnhCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbHinhAnh_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbHinhAnhCollection haCollection = new tbHinhAnhCollection();

            while (dr.Read())
            {
                tbHinhAnh hinhanh = new tbHinhAnh();
                hinhanh.MaHA = int.Parse(dr.GetValue(0).ToString());
                hinhanh.HinhAnh = dr.GetValue(1).ToString();
                hinhanh.MaSP = int.Parse (dr.GetValue(2).ToString());

                haCollection.Add(hinhanh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return haCollection;
        }
        public int imageFirst(int MaSanPham)
        {
            string sql = "select Min(MaHA) from tbHinhAnh as H where H.MaSP='" + MaSanPham + "'";
            SqlCommand cm = new SqlCommand(sql, cn);
            int kq = (int)cm.ExecuteScalar();
            return kq;
        }
       
    }
     
}